-- 棋盘部分
local Config = require("app.views.slots.Config")
local SymbolCfg = Config.SymbolCfg
local SlotsMainNode = class("SlotsMainNode", cc.Node)

function SlotsMainNode:ctor()
    self._cellSize = Config.CellSize
    self._contentSize = cc.size(Config.W * self._cellSize.width, Config.H * self._cellSize.height)

    self._reels = {}
    -- 所有的symbol节点，二维数组
    self._symbolModeMap = {}
    self._state = "idle"
    self._mainTaskEntry = require("app.views.slots.task.TaskEntry").new()
    self:_init()

    global.event:addEventListener("EVENT_START_SCROLL", self, self._onStartScroll) -- 点击按钮，开始转动
    global.event:addEventListener("EVENT_STOP_SCROLL", self, self._onStopScroll) -- 收到服务器返回的结果，准备停止转动
end

function SlotsMainNode:_init()
    local layout = ccui.Layout:create()
    self:addChild(layout)
    layout:setClippingEnabled(true)
    layout:setContentSize(self._contentSize)
    layout:setAnchorPoint(0.5, 0.5)

    local rootNode = display.newNode()
    layout:addChild(rootNode)
    self._rootNode = rootNode
    rootNode:setPosition(0, layout:getContentSize().height)

    local initData = global.data.initData.init
    for col, colData in ipairs(initData) do
        local reel = require("app.views.slots.ReelNode").new(col)
        rootNode:addChild(reel)
        reel:setPositionX((col-1) * self._cellSize.width)
        reel:initWithColData(colData)
        self._reels[col] = reel
    end
end
local maxSpeed = 5000
function SlotsMainNode:update(dt)
    self:_updateStates(dt)
    for _, reel in ipairs(self._reels) do
        reel:update(dt)
    end
    self._mainTaskEntry:updateEntry(dt)
end

function SlotsMainNode:_updateStates(dt)
    if self._state == "idle" then
        if self._scrollTrigger then
            self._scrollTrigger = false
            self._scrollingTimer = 0
            self._state = "scrolling"
        end
    elseif self._state == "scrolling" then
        self._scrollingTimer = self._scrollingTimer + dt

        for col, reel in ipairs(self._reels) do
            local speed = self:_getScrollSpeed(col, self._scrollingTimer)
            reel:setScrollSpeed(speed)
        end

        if self._scrollingTimer > 1.2 then
            for col, reel in ipairs(self._reels) do
                reel:stopScroll()
            end
            self._state = "scroll_stopping"
        end
    elseif self._state == "scroll_stopping" then
        local allStopped = true
        for col, reel in ipairs(self._reels) do
            if not reel:isStopped() then
                allStopped = false
                break
            end
        end
        if allStopped then 
            self._state = "check"
            -- 检查是否有消除
            local results = global.data.initData.results
            if not results or #results == 0 then
                self._state = "idle"
            else
                for i, v in ipairs(results) do
                    local delayEntry = require("app.views.slots.task.DelayEntry").new(1)
                    self._mainTaskEntry:addToQueue(delayEntry, delayEntry.updateEntry)
                    self:_handleClear(v.clear)
                    self:_handleFill(v.fill)
                end
                self._mainTaskEntry:addToQueue(nil, function (dt)
                    self._state = "idle"
                    return true
                end)
            end
        end
    elseif self._state == "check" then

    end
end

function SlotsMainNode:_getScrollSpeed(col, timer)
    timer = timer - (col-1) * 0.07
    if timer <= 0 then
        return 0
    elseif timer < 0.2 then
        return (400/0.2) * timer
    elseif timer < 0.8 then
        return (-maxSpeed-400)/(0.8-0.2) * timer
    else
        return -maxSpeed
    end
end

function SlotsMainNode:_onStartScroll()
    self._scrollTrigger = true
end

function SlotsMainNode:_onStopScroll()
    self._stopTrigger = true
end


function SlotsMainNode:_handleClear(clearData)
    local clearEntry = require("app.views.slots.task.ClearEntry").new(clearData, self._reels)
    self._mainTaskEntry:addToQueue(clearEntry, clearEntry.updateEntry)
end

function SlotsMainNode:_handleFill(fillData)
    local fillEntry = require("app.views.slots.task.FillEntry").new(fillData, self._reels)
    self._mainTaskEntry:addToQueue(fillEntry, fillEntry.updateEntry)
end

return SlotsMainNode